<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/BaseCms.php";

class News extends BaseCms
{
    public $tb_name = "news";
    public $currentPage = 1;
    public $pageSize = 20;
    public $totalDatas = 0;
    public $totalPages = 1;
    public $newsDatas;
    public $lastData;
    public $nextData;

    function __construct()
    {
        parent::__construct($this->tb_name);
    }

    /**
     * 初始化新闻页面信息
     * @param $categoryId 类别Id
     * @param int $currentPage 当前页
     * @param int $pageSize 每页显示数据
     */
    function init($categoryId, $currentPage = 1, $pageSize = 20)
    {
        $this->currentPage = $currentPage;
        $this->pageSize = $pageSize;
        $condition = ["id_data_category" => $categoryId];
        $orderBy = ["index_show"=>"asc","top_show"=>"asc","created_date" => "desc", "id_news" => "desc"];
        $this->newsDatas = $this->findByPage($this->currentPage, $this->pageSize, $condition, $orderBy);
        error_log(json_encode($this->newsDatas));
        $this->totalDatas = $this->count($condition);
        $this->totalPages = $this->totalPages % $this->pageSize == 0 ? (int)($this->totalPages / $this->pageSize) : (int)($this->totalDatas / $this->pageSize) + 1;
    }

    /**
     * 根据Id获取数据
     * @param $detail_id 数据主键
     */
    function loadById($detail_id)
    {
        $condition = ["id_news" => $detail_id];
        $orderBy = ["created_date" => "desc", "id_news" => "desc"];
        $this->newsDatas = $this->findOne($condition);
        $condition = ["id_news" => [$this->param_value_key => $detail_id, $this->param_operation => "<"]];
        $this->lastData = $this->findOne($condition, $orderBy);
        $orderBy = ["created_date" => "desc", "id_news" => "asc"];
        $condition = ["id_news" => [$this->param_value_key => $detail_id, $this->param_operation => ">"]];
        $this->nextData = $this->findOne($condition, $orderBy);
    }
}